class Solution
{
public:
    int minimumOperationsToWriteY(vector<vector<int>> &grid)
    {
        int n = grid.size();
        int mid = n / 2;
        auto isY = [&](int i, int j)
        {
            if (i >= mid && j == mid)
            {
                return true;
            }
            if (i < mid && i == j)
            {
                return true;
            }
            if (i < mid && i == n - j - 1)
            {
                return true;
            }
            return false;
        };

        vector<int> yPosNumCount(3, 0);
        vector<int> noneYPosNumCount(3, 0);
        int yPosCount = 0;
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                if (isY(i, j))
                {
                    ++yPosCount;
                    yPosNumCount[grid[i][j]]++;
                }
                else
                {
                    noneYPosNumCount[grid[i][j]]++;
                }
            }
        }

        int result = INT_MAX;
        int noneYPosCount = n * n - yPosCount;
        for (int yNum = 0; yNum <= 2; yNum++)
        {
            for (int noneYNum = 0; noneYNum <= 2; noneYNum++)
            {
                if (yNum == noneYNum)
                {
                    continue;
                }
                int yOpCount = yPosCount - yPosNumCount[yNum];
                int noneYOpCount = noneYPosCount - noneYPosNumCount[noneYNum];
                result = min(result, yOpCount + noneYOpCount);
            }
        }
        return result;
    }
};